<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <template #left>
        <van-icon name="arrow-left" size="18" @click="$router.back()"/>
      </template>
    </van-nav-bar>

    <!-- 输入 -->
    <van-form @submit="onSubmit" ref="userFormRef">
    <van-cell-group >
      <van-field 
        name="mobile"
        v-model="user.mobile"
        placeholder="请输入手机号"
        left-icon="phone-o"
        center
        maxlength="11"
        type="number"
        :rules="userFormRules.mobile">
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        left-icon="sign"
        center
        :rules="userFormRules.code">
        <template #button>
          <van-count-down v-if="isCountDownShow" :time="10*1000" format=" ss 秒" @finish="countDownFinish"/>
          <van-button v-else class="sms-send-btn" round size="small" type="primary"
          native-type="button"
          @click="smsSendAction"
          >发送验证码</van-button>
        </template>
        </van-field>
    </van-cell-group>
    <div class="login-btn-wrap">
      <van-button class="login-btn"  block type="primary" native-type="submit">
        登录
      </van-button>
    </div>
  </van-form>

  </div>
</template>

<script>
import { Login, SendSMS } from '@/apis/user.js'

import { showToast,showLoadingToast,showSuccessToast, showFailToast  } from 'vant';

export default {
  data() {
    return {
      user:{
        mobile:'13911111111',
        code:'246810'
        // mobile:'',
        // code:'',
      },
      userFormRules: {
        mobile: [
          { required: true, message: '请填写手机号码' },
          { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message:'手机号码格式不正确'}
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message:'验证码为6位'}
        ]
      },
      time:1000*10,
      isCountDownShow:false,
    }
  },
  methods: {
    async onSubmit() {

      showLoadingToast({
        message: '加载中...',
        forbidClick: true,
        // loadingType: 'spinner',
        duration:0
      });

      try {
        const {data: res} = await Login(this.user)
        console.log(res);
        this.$store.commit('setUsers',res.data)
        showSuccessToast('登录成功')
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          showFailToast('手机号或验证码错误')
        } else {
          showFailToast('登录失败' + error.message)
        }
      }
    },
    async smsSendAction () {
      try {
         await this.$refs.userFormRef.validate('mobile')
      } catch (error) {
          return console.log(error);
      }
      this.isCountDownShow = true
      
      try {
        await SendSMS(this.user.mobile)
      } catch (error) {
        console.log(error);
        this.isCountDownShow = false
        if (error.response.status === 429) {
          showToast('发送过于频繁')
        }
      }
      
    },
    countDownFinish () {
      this.isCountDownShow = false;
    }
  }
}
</script>

<style lang="less" scoped>
  .sms-send-btn {
    background-color: #ededed;
    color: #666;
    border: none;
    padding: 10px;
  }
  .login-btn-wrap {
    padding: 43px 13px;
    .login-btn {
      border: none;
      background-color: #6db4fb;
    }
  }
</style>